﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    class FrameTransfer
    {
        static public void UpdateOrigin(FrameData frameData)
        {
            AnimationData animationDataParent = frameData.Parent;
            ResourceData resourceDataParent = animationDataParent.Parent;

            var resourceName = resourceDataParent.ResourceName;
            var animationName = animationDataParent.Name;
            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(frameData);

            UpdateOrigin(resourceName, animationName, frameIndex, frameData.OriginX, frameData.OriginY);
        }

        static public void UpdateOrigin(string resourceName, string animationName, int frameIndex, float originX, float originY)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateFrameOrigin(resourceName, animationName, frameIndex, originX, originY));
        }

        static public void Insert(FrameData frameData)
        {
            AnimationData animationDataParent = frameData.Parent;
            var resourceName = animationDataParent.Parent.ResourceName;
            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(frameData);

            Insert(resourceName, animationDataParent.Name, frameIndex, frameData.Frame);
        }

        static public void Insert(string resourceName, string animationName, int frameIndex, Frame frame)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.InsertFrame(resourceName, animationName, frameIndex));
            UpdateOrigin(resourceName, animationName, frameIndex, frame.OriginX, frame.OriginY);

            for (int i = 0; i < frame.TextureList.Count; i++)
                TextureTransfer.Insert(resourceName, animationName, frameIndex, i, frame.TextureList[i]);
        }

        static public void Remove(AnimationData animationData, int index)
        {
            var resourceName = animationData.Parent.ResourceName;

            Remove(resourceName, animationData.Name, index);
        }

        static public void Remove(string resourceName, string animationName, int frameIndex)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveFrame(resourceName, animationName, frameIndex));
        }

        static public void Reorder(AnimationData animationData, int oldIndex, int newIndex)
        {
            var resourceName = animationData.Parent.ResourceName;

            Reorder(resourceName, animationData.Name, oldIndex, newIndex);
        }

        static public void Reorder(string resourceName, string animationName, int oldIndex, int newIndex)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.ReorderFrame(resourceName, animationName, oldIndex, newIndex));
        }
    }
}
